<?php

namespace Web\MainSiteBundle\Controller;


use BaseExt\Pagination;
use Model\CourseBundle\Model\CourseModel;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;


class WebController extends WebBaseController
{
    /**
     * @route("/",name="WebIndex")
     */
    public function WebIndex()
    {//网站首页
        $data = array();
        //获取所有课程分类
        $courseCategoryModel = new CourseCategoryModel($this->getDoctrine());
        $courseCategory = $courseCategoryModel->getAllCourseCategoryForWebIndex();
        $jsonCourseCategory = json_encode($courseCategory);
        $data["jsonCourseCategory"] = $jsonCourseCategory;
        $data["rootCourseCategory"] = $courseCategory[0];

        //课程列表
        $courseModel = new CourseModel($this->getDoctrine());
        //推荐课程
        $params["orderBy"]["course"]["isWebIndex"] = "DESC";
        $params["orderBy"]["course"]["isTop"] = "DESC";
        $list = $courseModel->getAdminLists($params,0,8);
        $data["recommend"] = $list;
        //分类1：小学
        $courseCategoryModel->getAllSonIds(3,$allCategory);
        $params[ "where" ]["course"]["category"]["in"] = $allCategory;
        $params["orderBy"]["course"]["isWebIndex"] = "DESC";
        $list = $courseModel->getListForWebIndex($params,0,8);
        $data["recommend"] = $list;
        return $this->render("WebMainSiteBundle:Web:index.html.twig",$data);
    }

    /**
     * @route("/Courses",name="WebCourseList")
     */
    public function WebCourseList(Request $request)
    {//全部课程列表
        if (!$p = $this->getParamRequired("page")) { $p = 1;}
        $perPage = 9;
        $data = array();
        //获取所有课程分类
        $courseCategoryModel = new CourseCategoryModel($this->getDoctrine());
        $courseCategory = $courseCategoryModel->getAllCourseCategoryForWebIndex();
        $jsonCourseCategory = json_encode($courseCategory);
        $data["jsonCourseCategory"] = $jsonCourseCategory;
        $data["rootCourseCategory"] = $courseCategory[0];
        $data["allCategory"]        = $courseCategoryModel->getAllCourseCategoryTree();
        //课程列表
        $courseModel = new CourseModel($this->getDoctrine());
        $params = array();
        //筛选分类
        if ($cat = $this->getParamRequired("cat")) {
            $courseCategoryModel->getAllSonIds($cat,$allCategory);
            $params[ "where" ]["course"]["category"]["in"] = $allCategory;
        }
        $params["orderBy"]["course"]["id"] = "DESC";
        if ($hot = $this->getParamRequired("hot")) {
            $params["orderBy"]["course"]["isHot"] = "DESC";
        }
        if ($time = $this->getParamRequired("addTime")) {
            switch ($time) {
                case 1 :
                    $params["orderBy"]["course"]["addTime"] = "DESC";
                    break;
                case 2 :
                    $params["orderBy"]["course"]["addTime"] = "ASC";
                    break;
                default:
            }
        }
        $listData = $courseModel->getListForWebIndex($params,$p-1,$perPage);

        $data["courses"] = $listData["data"];

        //分页
        $urlParams = $request->query->all();
        unset($urlParams["page"]);
        $baseUrl = $this->generateUrl("WebCourseList");
        $first_url = $this->generateUrl("WebCourseList",$urlParams);
        $pageParam["per_page"] = "$perPage";
        $pageParam["base_url"] = "$baseUrl";
        $pageParam["first_url"] = $first_url;
        $pageParam["total_rows"] = $listData["total"];
        $PageModel = new Pagination($pageParam);
        $page = $PageModel->create_links();
        $data["pageCourse"] = $page;
        $data["curPage"] = $p;

        return $this->render("WebMainSiteBundle:Web:course_list.html.twig",$data);
    }

}
